//
//  SupplementaryInformationViewController.m
//  Binfen
//
//  Created by NigasMone on 14-12-1.
//  Copyright (c) 2014年 NigasMone. All rights reserved.
//
#import "SupplementaryInformationViewController.h"
#import "CameraActionSheet.h"
#import "TextEditController.h"
#import "BaseTableViewCell.h"
#import "Globals.h"
#import "KLocation.h"
#import "VPImageCropperViewController.h"
#import "UIImage+Resize.h"
#import "UIImageView+WebCache.h"
#import "ImageTouchView.h"
#import "KLocatePickView.h"
#import "AppDelegate.h"
#import "UIImage+FlatUI.h"
#import "YMParameterCellObj.h"
#import "YMParametersTableViewCell.h"
#import "KAlertView.h"
#import "MyAddressViewController.h"
#import "MyCompanyListViewController.h"
#import "MyQRCodeViewController.h"
#import "QRCodeGenerator.h"
#define fontSize 12

@interface SupplementaryInformationViewController ()<CameraActionSheetDelegate,TextEditControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, ImageTouchViewDelegate>
{
    ImageTouchView * headView;
    CGFloat height;
    BOOL hasImage;
    
//    NSString * realName;//真实姓名
    NSString * name;
    NSString * gender;
    NSString * province;
    NSString * city;
    NSString * sign;
    
    NSMutableArray *cellObjs;

}

@end

@implementation SupplementaryInformationViewController
{
    UITableView *tableView;
}
@synthesize user, editType;

- (id)init {
    if (self = [super init]) {
        // Custom initialization
        cellObjs = [NSMutableArray array];
        headView = [[ImageTouchView alloc] initWithFrame:CGRectZero delegate:self];
        headView.image = [Globals getImageUserHeadDefault];
        editType = forEditInfo;
    }
    return self;
}

- (void)dealloc {
    Release(headView);
    self.user = nil;
}


- (void)showBackButton {
    
}


- (void)viewDidLoad {
    if (editType != forEditInfo) {
        self.willShowBackButton = NO;
        [self.navigationItem setHidesBackButton:YES];
    }
//

//    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = RGBCOLOR(243, 243, 243);
    [self setRightBarButton:@"确定" selector:@selector(saveBtnPressed)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(saveBtnPressed)];
    self.user = [[BSEngine currentEngine] user];
    if (editType == forEditInfo) {
        self.navigationItem.title = @"编辑资料";
    } else {
        self.navigationItem.title = @"完善资料";
    }
    if (user.headsmall && user.headsmall.length > 0) {
        hasImage = YES;
    } else {
        hasImage = NO;
    }
    
    tableView  =[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    

    //    menuTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    tableView.delegate = (id)self;
    tableView.dataSource = (id)self;
    
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo (self.view);
        make.right.equalTo (self.view);
        make.top.equalTo (self.view);
        make.bottom.equalTo (self.view);
        
    }];
    
    [tableView registerClass:[YMParametersTableViewCell class] forCellReuseIdentifier:@"YMParametersTableViewCell"];
    
    headView.layer.masksToBounds = YES;
    headView.layer.cornerRadius = 5;
    DLog(@"user.headsmall = %@",user.headsmall);
    [headView sd_setImageWithUrlString:user.headsmall placeholderImage:[Globals getImageUserHeadDefault]];
    

    name = user.nickname;
    gender = user.gender;
    province = user.province;
    city = user.city;
    sign = user.sign;
    [self setupData];
    
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [tableView reloadData];

}
-(void)setupData
{
    
    cellObjs = [NSMutableArray array];
    NSMutableArray *array = [NSMutableArray array];
    YMParameterCellObj *obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeCustomView];
    obj.accessoryView=headView;
    obj.title = @"头像";
    obj.cellHeigth =70;
    obj.accessoryViewWidth= 60;
    
//    obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [array addObject:obj];
    [cellObjs addObject:array];
    array = [NSMutableArray array];
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
    obj.name=obj.title = @"昵称";
    obj.isRequired = YES;
    obj.value = user.nickname;
    obj.cellAction = @selector(editUserNick:);
    obj.key = @"nickname";
    obj.cellHeigth =45;
    obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [array addObject:obj];
    if(editType == forEditInfo)
    {
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"云库号";
        obj.isRequired = YES;
        obj.value = user.kunumber;
        
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryNone;
        [array addObject:obj];
        
        
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeCustomView];
        obj.title = @"二维码";
        obj.cellHeigth =45;
        UIImageView *codeImageView = [[UIImageView alloc] init];
        CGFloat width =35;
        codeImageView.image = [QRCodeGenerator qrImageForString:[KUserPer stringByAppendingString: [[BSEngine currentEngine] user].uid ] imageSize:width];
        obj.accessoryView = codeImageView;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        obj.pushToClass = NSClassFromString(@"MyQRCodeViewController");
        obj.accessoryViewWidth=width;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"手机号";
        obj.isRequired = NO;
        obj.value = user.phone;
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryNone;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"我的地址";
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        obj.pushToClass = NSClassFromString(@"MyAddressViewController");
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"单位公司资料";
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        obj.pushToClass = NSClassFromString(@"MyCompanyListViewController");
        
        [array addObject:obj];
        [cellObjs addObject:array];
        array = [NSMutableArray array];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"性别";
        if (user.gender.intValue == 0) {
            obj.value= @"男";
        } else if (gender.intValue == 1) {
            obj.value= @"女";
        }
        else{
            obj.value= @"选填";
        }
        obj.cellAction = @selector(showSexPicker:);
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"地区";
        obj.cellHeigth =45;
        if ( user.province.length == 0) {
            obj.value = @"选填";
        } else {
            obj.value = [NSString stringWithFormat:@"%@ %@",  user.province,  user.city];
        }
        obj.cellAction = @selector(showLoctionPicker:);
        
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"签名";
        obj.cellHeigth =45;
        obj.value = user.sign;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        obj.cellAction = @selector(editSign:);
        [array addObject:obj];
        [cellObjs addObject:array];
    }
    else{
        
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.name=obj.title = @"真实姓名";
        obj.isRequired = YES;
        obj.key = @"name";
        obj.value = nil;
        obj.cellHeigth =45;
        obj.cellAction = @selector(editUserName:);
        
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [array addObject:obj];
        
        
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"性别";
        if (user.gender.intValue == 0) {
            obj.value= @"男";
        } else if (user.gender.intValue == 1) {
            obj.value= @"女";
        }
        else{
            obj.value= @"选填";
        }
        
        obj.cellAction = @selector(showSexPicker:);
        obj.cellHeigth =45;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.title = @"地区";
        obj.cellHeigth =45;
        if ( user.province.length == 0) {
            obj.value = @"选填";
        } else {
            obj.value = [NSString stringWithFormat:@"%@ %@",  user.province,  user.city];
        }
        obj.cellAction = @selector(showLoctionPicker:);
        
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.name=obj.title = @"签名";
        obj.key = @"sign";
        obj.cellHeigth =45;
        obj.value = user.sign;
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        obj.cellAction = @selector(editSign:);
 
        [array addObject:obj];
        [cellObjs addObject:array];
        array = [NSMutableArray array];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.name= obj.title = @"安全问题";
          obj.key =@"safeissue";
        obj.cellHeigth =45;
        obj.isRequired = YES;
        obj.value = nil;
        obj.cellAction = @selector(editCellText:);
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        [array addObject:obj];
        
        obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        obj.name= obj.title = @"安全回答";
        obj.key =@"safeanswer";
        obj.isRequired = YES;
        obj.value = nil;
        obj.cellHeigth =45;
        obj.cellAction = @selector(editCellText:);
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        [array addObject:obj];
        
        
        
        [cellObjs addObject:array];
    }
    
}

- (void)saveBtnPressed {
    [self sendRequest];
}

#pragma mark - ImageTouchViewDelegate,点击头像--
- (void)imageTouchViewDidSelected:(id)sender {
    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
    [actionSheet show];
}

#pragma mark - kPickerView
- (void)showPickerView:(NSInteger)row {
    if (row == 1) {
        CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"男", @"女", nil];
        actionSheet.tag = 11;
        [actionSheet show];
    } else {
        KLocatePickView *locateView = [[KLocatePickView alloc] initWithTitle:@"选择城市" delegate:self];
        [locateView showInView:self.view];
    }
}
#pragma mark --底部弹出男女选择器--
-(void)showSexPicker:(NSIndexPath *)indexPath
{
    YMParameterCellObj *cellObj = cellObjs[indexPath.section][indexPath.row];
    
    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"男", @"女", nil];
    actionSheet.tag = 11;
        actionSheet.userInfo =cellObj;
    [actionSheet show];
}
#pragma mark --弹出城市选择器--
-(void)showLoctionPicker:(NSIndexPath *)indexPath
{
    YMParameterCellObj *cellObj = cellObjs[indexPath.section][indexPath.row];
    
    KLocatePickView *locateView = [[KLocatePickView alloc] initWithTitle:@"选择城市" delegate:self];
    locateView.userInfo =cellObj;
    [locateView showInView:self.view];
}
#pragma mark --编辑昵称--
-(void)editUserNick:(NSIndexPath *)indexPath
{
        TextEditType tet = TextEditTypeDefault;
        NSString * str = nil;
        
        TextEditController *con = [[TextEditController alloc] initWithDel:self type:tet title:@"昵称" value:nil];
        con.indexPath = indexPath;
        con.maxTextCount = 200;
        
        str = name;
        con.maxTextCount = 8;
        con.minTextCount = 2;
    
        con.editType = tet;
        con.defaultValue = str;
        [self pushViewController:con];

}
-(void)editUserName:(NSIndexPath *)indexPath
{
    TextEditType tet = TextEditTypeDefault;
    
    YMParameterCellObj *cellObj = cellObjs[indexPath.section][indexPath.row];
    
    NSString * str = cellObj.value;
    
    TextEditController *con = [[TextEditController alloc] initWithDel:self type:tet title:@"真实姓名" value:nil];
    con.indexPath = indexPath;
    con.maxTextCount = 200;
    
    str =  cellObj.value;
    con.maxTextCount = 6;
    con.minTextCount = 2;
    
    con.editType = tet;
    con.defaultValue = str;
    [self pushViewController:con];
    
}

-(void)editCellText:(NSIndexPath *)indexPath
{
    TextEditType tet = TextEditTypeDefault;
    
    YMParameterCellObj *cellObj = cellObjs[indexPath.section][indexPath.row];
    NSString * str = cellObj.value;
    
    TextEditController *con = [[TextEditController alloc] initWithDel:self type:tet title:cellObj.title value:nil];
    con.indexPath = indexPath;
    con.maxTextCount = 200;
    
    str =  cellObj.value;
    con.maxTextCount = 20;
    con.minTextCount = 2;
    
    con.editType = tet;
    con.defaultValue = str;
    [self pushViewController:con];
}
-(void)editSign:(NSIndexPath *)indexPath
{
    TextEditType tet = TextEditTypeDefault;
    NSString * str = nil;
    
    TextEditController *con = [[TextEditController alloc] initWithDel:self type:tet title:@"昵称" value:nil];
    con.indexPath = indexPath;
    con.maxTextCount = 200;
    
    str = sign;
    con.maxTextCount = 30;
    tet = TextEditTypeMultipleLines;

    con.editType = tet;
    con.defaultValue = str;
    [self pushViewController:con];
    
}

#pragma mark - UITableViewDataSource

- (CGFloat)tableView:(UITableView *)sender heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    YMParameterCellObj *obj = cellObjs[indexPath.section][indexPath.row];
    return obj.cellHeigth;
}

- (NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section {
    return [cellObjs[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return cellObjs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
- (void)tableView:(UITableView *)sender willDisplayCell:(BaseTableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark - UITableViewDelegate

- (UITableViewCell*)tableView:(UITableView *)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier =@"YMParametersTableViewCell";
    
    YMParametersTableViewCell *cell = [sender dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    YMParameterCellObj* obj =  cellObjs[indexPath.section][indexPath.row];
    [cell setupViewFor:obj];
    return cell;
    
    /*
    static NSString* CellIdentifier = @"BaseHeadCell";
    BaseTableViewCell* cell = [sender dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = RGBCOLOR(80, 80, 80);
        cell.detailTextLabel.textColor = RGBCOLOR(163, 163, 163);
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.font =
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
    }
    cell.backgroundColor =
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.imageView.hidden = YES;
    UIImageView *badgeIcon = (UIImageView*)[cell.contentView viewWithTag:1000];
    if (!badgeIcon) {
        badgeIcon = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.width - 70, 4, self.tableViewCellHeight - 8, self.tableViewCellHeight - 8)];
        badgeIcon.tag = 1000;
        badgeIcon.image = [Globals getImageUserHeadDefault];
    }
    if (badgeIcon) {
        [cell.contentView addSubview:badgeIcon];
    }
    badgeIcon.hidden = YES;
    cell.detailTextLabel.text = @"";
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text =
            cell.className = @"昵称";
            if (name.length == 0) {
                cell.detailTextLabel.text = @"必填";
            } else {
                cell.detailTextLabel.text = name;
            }
            break;
        case 1:
            cell.textLabel.text =
            cell.className = @"性别";
            cell.detailTextLabel.textColor = [UIColor grayColor];
            
            if (gender.intValue == 0) {
                cell.detailTextLabel.text = @"男";
            } else if (gender.intValue == 1) {
                cell.detailTextLabel.text = @"女";
            } else {
                cell.detailTextLabel.text = @"选填";
            }
            break;
        case 2:
            cell.textLabel.text =
            cell.className = @"地区";
            if (province.length == 0) {
                cell.detailTextLabel.text = @"选填";
            } else {
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", province, city];
            }
            break;
        case 3:
            cell.textLabel.text =
            cell.className = @"个性签名";
            
            if (sign.length == 0) {
                if (editType == forSupplementaryInfo) {
                    cell.detailTextLabel.text = @"选填";
                }
            } else {
                cell.detailTextLabel.text = sign;
            }
            break;
        case 4:
            cell.textLabel.text =
            cell.className = @"二维码";
            
            if (sign.length == 0) {
                if (editType == forSupplementaryInfo) {
                    cell.detailTextLabel.text = @"选填";
                }
            } else {
                cell.detailTextLabel.text = sign;
            }
            break;
        case 5:
            cell.textLabel.text =
            cell.className = @"我的地址";
            
            if (sign.length == 0) {
                if (editType == forSupplementaryInfo) {
                    cell.detailTextLabel.text = @"选填";
                }
            } else {
                cell.detailTextLabel.text = sign;
            }
            break;
        case 6:
            cell.textLabel.text =
            cell.className = @"单位公司资料";
            
            if (sign.length == 0) {
                if (editType == forSupplementaryInfo) {
                    cell.detailTextLabel.text = @"选填";
                }
            } else {
                cell.detailTextLabel.text = sign;
            }
            break;
        default:
            break;
    }

    [cell addArrowRight];
    [cell update:^(NSString *name) {
        cell.detailTextLabel.left = cell.width - 220;
        cell.detailTextLabel.width = 190;
        cell.topLineView.frame = CGRectMake(10, 0.5, cell.width - 10, 0.5);
        UIImage * image = nil;
        if (indexPath.row == 0) {
            image = [UIImage imageWithColor:RGBCOLOR(217, 217, 217) cornerRadius:0];
        } else {
            image = [UIImage imageWithColor:RGBCOLOR(235, 235, 235) cornerRadius:0];
        }
        cell.topLineView.image = image;
    }];
    return cell;
     */
}


- (void)pushViewController:(id)con {
    ((BaseViewController*)con).hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:con animated:YES];
}

- (void)pushEidt:(NSIndexPath*)indexPath title:(NSString*)title value:(NSString*)value {
    TextEditType tet = TextEditTypeDefault;
    NSString * str = nil;
    
    TextEditController *con = [[TextEditController alloc] initWithDel:self type:tet title:title value:nil];
    con.indexPath = indexPath;
    con.maxTextCount = 200;
    
    if (indexPath.row == 0 && indexPath.section==1) {
        str = name;
        con.maxTextCount = 8;
        con.minTextCount = 2;
    } else if (indexPath.row == 3 && indexPath.section==1) {
        str = sign;
        con.maxTextCount = 30;
        tet = TextEditTypeMultipleLines;
    } else {
        str = value;
    }
    con.editType = tet;
    con.defaultValue = str;
    [self pushViewController:con];
}

- (void)textEditControllerDidEdit:(NSString*)text idx:(NSIndexPath *)indexPath {
    
    YMParameterCellObj *obj = cellObjs[indexPath.section][indexPath.row];
    obj.value = text;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [sender deselectRowAtIndexPath:indexPath animated:YES];
//    YMParametersTableViewCell* cell = (YMParametersTableViewCell*)[sender cellForRowAtIndexPath:indexPath];
   
    YMParameterCellObj *cellObj = cellObjs[indexPath.section][indexPath.row];
    
    if(cellObj.cellAction)
    {
        [self performSelector:cellObj.cellAction withObject:indexPath];
    }
    else if(cellObj.pushToClass)
    {
        UIViewController *controller = [[[cellObj pushToClass] alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
    }
//    if (indexPath.row == 0 || indexPath.row == 3) {
////        [self pushEidt:indexPath title:cell.c value:cell.detailTextLabel.text];
//    } else {
//        [self showPickerView:indexPath.row];
//    }
}
- (void)showText:(id)text {
    [KAlertView showType:KAlertTypeNone text:text for:1.45 animated:YES];
}

#pragma mark - Request

- (BOOL)sendRequest {
    
    for (NSArray*sections in cellObjs) {

        for (YMParameterCellObj *obj in sections) {
            NSString *errorStr = [obj check];
            if (errorStr.length >0) {
                [self showText:errorStr];
                return NO;
            }
        }
    }
    
        if ([super startRequest]) {
            NSMutableDictionary * dic = [NSMutableDictionary dictionary];
            
            for (NSArray*sections in cellObjs) {
                
                for (YMParameterCellObj *obj in sections) {
                 if(obj.value && obj.key)
                 {
                    [dic setObject:obj.value forKey:obj.key];
                 }
                }
            }
        
            [dic setValue:gender forKey:@"gender"];
            [dic setValue:province forKey:@"province"];
            [dic setValue:city forKey:@"city"];
        
            if(editType == forEditInfo)
            {
            [dic setValue:user.name forKey:@"name"];
            [dic setValue:user.safeanswer forKey:@"safeanswer"];
            [dic setValue:user.safeissue forKey:@"safeissue"];
            }
            
            
            [client editUserInfo:(hasImage?headView.image:nil) user:dic];
        }

    return YES;
}

- (BOOL)requestDidFinish:(id)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        id data = [obj getDictionaryForKey:@"data"];
        User* item = [User objWithJsonDic:data];
        if (item) {
            [[BSEngine currentEngine] setCurrentUser:item password:[BSEngine currentEngine].passWord];
            [self showText:@"保存成功"];
            if (editType == forEditInfo) {
                [self popViewController];
            } else {
                [self dismissModalController:YES];
            }
            
            [[BSEngine currentEngine] readAuthorizeData];
            return YES;
        }
    }
    return NO;
}

#pragma mark - CameraActionSheetDelegate

- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2) {
        return;
    }
    if (sender.tag == 11) {
        gender = [NSString stringWithFormat:@"%d", (int)buttonIndex];
        YMParameterCellObj *obj=sender.userInfo;
        if (gender.intValue == 0) {
            obj.value= @"男";
        } else if (gender.intValue == 1) {
            obj.value= @"女";
        }
        else{
            obj.value= @"必填";
        }
        [tableView reloadData];
    } else {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        if (buttonIndex == 0){
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        } else if (buttonIndex == 1) {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            } else {
//                [self showText:@"无法打开相机"];
            }
        }
        [self presentModalController:picker animated:YES];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        img = [UIImage rotateImage:img];
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:img cropFrame:CGRectMake((self.view.frame.size.width - 240)/2, 100.0f, 240, 240) limitScaleRatio:3.0 title:@"上传头像"];
        [imgCropperVC setCompletionBlock:^(BOOL didFinished, UIImage *editedImage) {
            if (editedImage) {
                hasImage = YES;
                headView.image = editedImage;
                [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            }
        }];
        [self pushViewController:imgCropperVC];
    }];
}

- (void)previewImageDid:(id)image{
    if (image) {
        hasImage = YES;
        headView.image = image;
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 1) {
    
        KLocatePickView *locateView = (KLocatePickView *)actionSheet;
        KLocation *location = locateView.locate;
        province = location.state;
        city = location.city;
        
        
        YMParameterCellObj *obj=((KLocatePickView *)actionSheet).userInfo;
        obj.value = [NSString stringWithFormat:@"%@ %@",  province,  city];
        [tableView reloadData];
    }
}
@end
